/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jVid.java
 *
 * Created on Jun 5, 2010, 4:08:08 PM
 */

package jvid;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;


/**
 *
 * @author alejandro
 */
public class jVid extends javax.swing.JFrame {

    public File selectedFile;
    /** Creates new form jVid */
    public jVid() {
        initComponents();
        sc = new serverChoose();
   
        /*
        JMenuBar menubar = new JMenuBar();
        this.setJMenuBar(menubar);

        JMenu file = new JMenu("Archivo");
        menubar.add(file);
        JMenuItem exit1 = new JMenuItem("Salir...");
        file.add(exit1);

        JMenu help = new JMenu("Ayuda");
        menubar.add(help);
        JMenuItem about = new JMenuItem("Acerca de...");
        help.add(about);

        class exit1action implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }

        class helpaction implements ActionListener{
            public void actionPerformed(ActionEvent e){
                
            }
        }
        
        exit1.addActionListener(new exit1action());
        help.addActionListener(new helpaction());
        */
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        local = new javax.swing.JPanel();
        verVideo = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        videoPath = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        network = new javax.swing.JPanel();
        servidor = new javax.swing.JButton();
        disconnect = new javax.swing.JButton();
        verVideos = new javax.swing.JButton();
        feedback = new javax.swing.JLabel();
        exit = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        salirMenu = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jVideo - Mira tus videos local y remotamente");
        setLocation(new java.awt.Point(0, 0));
        setLocationByPlatform(true);
        setResizable(false);

        jLabel6.setText("jLabel6");

        local.setBorder(javax.swing.BorderFactory.createTitledBorder("Ver videos locales"));

        verVideo.setText("Ver video");
        verVideo.setEnabled(false);
        verVideo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirVLC(evt);
            }
        });

        jButton1.setText("Cargar...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirArchivo(evt);
            }
        });

        videoPath.setEnabled(false);

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel3.setText("Nombre:");
        jLabel3.setEnabled(false);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel1.setText("Tamaño:");
        jLabel1.setEnabled(false);

        jLabel7.setMaximumSize(new java.awt.Dimension(157, 0));
        jLabel7.setMinimumSize(new java.awt.Dimension(157, 0));

        javax.swing.GroupLayout localLayout = new javax.swing.GroupLayout(local);
        local.setLayout(localLayout);
        localLayout.setHorizontalGroup(
            localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(localLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, localLayout.createSequentialGroup()
                        .addComponent(videoPath, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(verVideo))
                    .addGroup(localLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                        .addGap(15, 15, 15))
                    .addGroup(localLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                        .addGap(14, 14, 14))))
        );
        localLayout.setVerticalGroup(
            localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(localLayout.createSequentialGroup()
                .addGroup(localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verVideo)
                    .addComponent(jButton1)
                    .addComponent(videoPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(localLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        network.setBorder(javax.swing.BorderFactory.createTitledBorder("Ver videos remotos"));

        servidor.setText("Servidor...");
        servidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                servidorActionPerformed(evt);
            }
        });

        disconnect.setText("Desconectar");
        disconnect.setEnabled(false);
        disconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disconnectActionPerformed(evt);
            }
        });

        verVideos.setText("Ver videos...");
        verVideos.setEnabled(false);
        verVideos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verVideosActionPerformed(evt);
            }
        });

        feedback.setFont(new java.awt.Font("Lucida Grande", 0, 10));
        feedback.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        feedback.setText(" ");

        javax.swing.GroupLayout networkLayout = new javax.swing.GroupLayout(network);
        network.setLayout(networkLayout);
        networkLayout.setHorizontalGroup(
            networkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(networkLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(networkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(networkLayout.createSequentialGroup()
                        .addComponent(servidor)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(disconnect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                        .addComponent(verVideos))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, networkLayout.createSequentialGroup()
                        .addComponent(feedback, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        networkLayout.setVerticalGroup(
            networkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(networkLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(networkLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verVideos)
                    .addComponent(servidor)
                    .addComponent(disconnect))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(feedback)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        exit.setText("Salir");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        jMenu1.setText("Archivo");

        salirMenu.setText("Salir...");
        salirMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirMenuActionPerformed(evt);
            }
        });
        jMenu1.add(salirMenu);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        jMenuItem1.setText("Acerca de...");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exit, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(local, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(network, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(local, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 5, Short.MAX_VALUE)
                .addComponent(network, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(exit)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void abrirArchivo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirArchivo
        JFileChooser fileChooser = new JFileChooser(".");
        FileFilter filter1 = new ExtensionFileFilter("Formatos de video - flv, mp4, avi, ogg, mpeg, wmv", new String[] { "flv","mp4","avi","ogg","mpeg","wmv" });
        fileChooser.setFileFilter(filter1);
        int status = fileChooser.showOpenDialog(null);
        if (status == JFileChooser.APPROVE_OPTION) {
          selectedFile = fileChooser.getSelectedFile();

          videoPath.setText(selectedFile.getPath());
          verVideo.setEnabled(true);

          System.out.println(selectedFile.getParent());
          System.out.println(selectedFile.getName());
          jLabel7.setText(selectedFile.getName());
          jLabel3.setEnabled(true);
          jLabel1.setEnabled(true);
          jLabel8.setText(Long.toString(selectedFile.length()/1000)+" Kb");
        }
    }//GEN-LAST:event_abrirArchivo

    private void abrirVLC(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirVLC
        try {
            //if (selectedFile) {
            String path = selectedFile.getCanonicalPath();
            System.out.println(path);
            String[] cmd = { "vlc", selectedFile.getCanonicalPath() };
            Runtime.getRuntime().exec(cmd);
            //Runtime.getRuntime().exec("vlc "+"\""+selectedFile.getCanonicalPath()+"\"");
            //}
        } catch (IOException ex) {
            Logger.getLogger(jVid.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_abrirVLC

    private void servidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_servidorActionPerformed
        sc.setModal(true);
        sc.setVisible(true);
        sc.clearFeedBack();
        try{
            if (!sc.videoTrans.isClosed()){
                disconnect.setEnabled(true);
                verVideos.setEnabled(true);
                servidor.setEnabled(false);
                feedback.setText("Host: "+sc.videoTrans.getInetAddress().getHostAddress()+" Puerto: "+sc.videoTrans.getPort());
            }
        } catch (Exception E){

        }
    }//GEN-LAST:event_servidorActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        System.exit(1);
    }//GEN-LAST:event_exitActionPerformed

    private void disconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disconnectActionPerformed
        if(sc.doDisconnect()){
            servidor.setEnabled(true);
            verVideos.setEnabled(false);
            disconnect.setEnabled(false);
            feedback.setText("Desconectado existosamente de "+sc.videoTrans.getInetAddress().getHostAddress());
        } else {
            feedback.setText("Error al desconectar, espere y vuelva a intentarlo o cierre el programa");
        }
    }//GEN-LAST:event_disconnectActionPerformed

    private void salirMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirMenuActionPerformed
        System.exit(0);
    }//GEN-LAST:event_salirMenuActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
         JOptionPane.showMessageDialog(this,"Fernando Velasquez \nCarlos Chitty \nAlejandro Rodriguez \nDavid moreno \nYessica De Ascencao \nEli Segovia \n","Desarrollado por",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void verVideosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verVideosActionPerformed
        watchVideosOnline videos = new watchVideosOnline(sc.videoTrans,sc.out,sc.in);
        videos.setModal(true);
        videos.setVisible(true);

        try{
            sc.doDisconnect();
            servidor.setEnabled(true);
            verVideos.setEnabled(false);
            disconnect.setEnabled(false);
            feedback.setText("Desconectado existosamente de "+sc.videoTrans.getInetAddress().getHostAddress());
        } catch (Exception e) {
            feedback.setText("Error al desconectar, espere y vuelva a intentarlo o cierre el programa");
        }
    }//GEN-LAST:event_verVideosActionPerformed
    

    class ExtensionFileFilter extends FileFilter {
      String description;

      String extensions[];

      public ExtensionFileFilter(String description, String extension) {
        this(description, new String[] { extension });
      }

      public ExtensionFileFilter(String description, String extensions[]) {
        if (description == null) {
          this.description = extensions[0];
        } else {
          this.description = description;
        }
        this.extensions = (String[]) extensions.clone();
        toLower(this.extensions);
      }

      private void toLower(String array[]) {
        for (int i = 0, n = array.length; i < n; i++) {
          array[i] = array[i].toLowerCase();
        }
      }

      public String getDescription() {
        return description;
      }

      public boolean accept(File file) {
        if (file.isDirectory()) {
          return true;
        } else {
          String path = file.getAbsolutePath().toLowerCase();
          for (int i = 0, n = extensions.length; i < n; i++) {
            String extension = extensions[i];
            if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
              return true;
            }
          }
        }
        return false;
      }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(jVid.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(jVid.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(jVid.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(jVid.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jVid().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton disconnect;
    private javax.swing.JButton exit;
    private javax.swing.JLabel feedback;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel local;
    private javax.swing.JPanel network;
    private javax.swing.JMenuItem salirMenu;
    private javax.swing.JButton servidor;
    private javax.swing.JButton verVideo;
    private javax.swing.JButton verVideos;
    private javax.swing.JTextField videoPath;
    // End of variables declaration//GEN-END:variables
    public serverChoose sc;
}
